package main

import (
	"fmt"
	"hxzkx/global/model"
	"hxzkx/inits"
	order "hxzkx/kitex_gen/hertz/order/paymentservice"
	"log"
	"net"
	"os"
	"time"

	"github.com/cloudwego/kitex/server"
	"github.com/redis/go-redis/v9"
)

func main() {
	path := "conf"
	if len(os.Args) > 1 {
		path = os.Args[1]
	}
	inits.Init(path)
	go ExpireWith()
	addr, _ := net.ResolveTCPAddr("tcp", "127.0.0.1:50052")
	svr := order.NewServer(new(PaymentServiceImpl), server.WithServiceAddr(addr))

	err := svr.Run()

	if err != nil {
		log.Println(err.Error())
	}
}

func ExpireWith() {
	ExipreOrderKey := fmt.Sprintf("order_notpay")
	ticker := time.NewTicker(60 * time.Second)
	for _ = range ticker.C {
		exipre := time.Now().Add(-15 * time.Minute).Unix()
		pay_orders, _ := model.RDB.ZRangeByScore(model.Ctx, ExipreOrderKey, &redis.ZRangeBy{
			Min: fmt.Sprintf("%d", 0),
			Max: fmt.Sprintf("%d", exipre),
		}).Result()
		model.DB.Model(&model.PaymentOrder{}).Joins("join orders on orders.id = payment_orders.order_id").Where("payment_orders.payment_order_id in (?)", pay_orders).Update("orders.status", "5")
	}

}
